<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>GenerateRendererTask</title>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/dijit/themes/tundra/tundra.css"/>
    <link  rel="stylesheet"  type="text/css" href="http://localhost/arcgis_js_api/library/3.17/3.17/esri/css/esri.css" />
    <script  type="text/Javascript" src="http://localhost/arcgis_js_api/library/3.17/3.17/init.js"></script>
    <style type="text/css">
        .MapClass{
            width:100%;
            height:500px;
            border:1px solid #000;
        }
    </style>
    <script type="text/javascript">

        require(["esri/map",
            "esri/layers/ArcGISDynamicMapServiceLayer",
            "dojo/dom",
            "dojo/on",
            "esri/tasks/GenerateRendererTask",
            "esri/layers/FeatureLayer",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/tasks/GenerateRendererParameters",
            "esri/tasks/ClassBreaksDefinition",
            "esri/tasks/AlgorithmicColorRamp",
            "dojo/domReady!"], function
                (Map,
                 ArcGISDynamicMapServiceLayer,
                 dom,on,
                 GenerateRendererTask,
                 FeatureLayer,
                 SimpleFillSymbol,
                 SimpleLineSymbol,
                 GenerateRendererParameters,
                 ClassBreaksDefinition,
                 AlgorithmicColorRamp
                ) {
            //服务器端渲染
            var MyMap = new Map("MyMapDiv");
            var layer = new ArcGISDynamicMapServiceLayer
            ("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer/");
            MyMap.addLayer(layer)
            //获得id为Btn的DOM对象
            var btn=dom.byId("Btn");
            //给btn绑定点击事件
            on(btn,"click",function(e){
                //定义线符号
                var lineSymbol=new SimpleLineSymbol(SimpleLineSymbol.STYLE_DASH, new dojo.Color([255, 0, 0]), 3);
                //定义面符号
                var fill=new SimpleFillSymbol(SimpleFillSymbol.STYLE_SOLID, lineSymbol,new dojo.Color("#FFFFCC"));
                //创建服务器渲染对象
                var RenderAnalyst = new GenerateRendererTask("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer/1");
                //创建服务器渲染参数对象
                var params = new GenerateRendererParameters();   
                //创建颜色坡道对象
                var colorRamp = new AlgorithmicColorRamp();
                //颜色的起始位置
                colorRamp.fromColor = dojo.colorFromHex("#998ec3");
                //颜色的终止位置
                colorRamp.toColor = dojo.colorFromHex("#f1a340");
                //颜色的类型
                colorRamp.algorithm = "hsv";
                //告诉服务器我们需要什么样子的渲染器
                var unidef = new ClassBreaksDefinition();
                //分类的字段
                unidef.classificationField = "Shape.STArea()";
                //如何分类
                unidef.classificationMethod="natural-breaks";
                //分为几类？
                unidef.breakCount = 3;
                //默认的符号
                unidef.baseSymbol=fill;
                //将自定义的颜色坡道告诉服务器
                unidef.colorRamp=colorRamp;
                //给渲染参数对象赋值
                params.classificationDefinition = unidef;
                //进行服务器渲染
                RenderAnalyst.execute(params, showRender);


            });
            //处理服务器返回来的结果
            function showRender(renderer) {

                if (renderer != null) {
                    //定义要素图层，注意：这个地方我们用到并不是要素服务，而是地图服务
                    var featureLayer = new FeatureLayer("http://localhost:6080/arcgis/rest/services/Test/ft/MapServer/1",{
                        mode:  FeatureLayer.MODE_SNAPSHOT,
                        outFields: ["*"]
                    });     
                    //赋予渲染器
                    featureLayer.setRenderer(renderer);
                    featureLayer.refresh();
                    MyMap.addLayer(featureLayer);
                }
            }






        });


    </script>
</head>
<body>
<div id="MyMapDiv" class="MapClass"></div>
<input type="button" id="Btn" value="服务器渲染" />
</body>
</html>